package cn.xtits.design.controller;

import cn.xtits.design.entity.LabelTemplate;
import cn.xtits.design.entity.LabelTemplateExample;
import cn.xtits.design.entity.Pagination;
import cn.xtits.design.service.LabelTemplateService;
import cn.xtits.design.util.GenerateGUID;
import cn.xtits.xtf.common.utils.JsonUtil;
import cn.xtits.xtf.common.web.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @fileName: LabelTemplateController.java
 * @author: Dan
 * @createDate: 2019-10-08 16:23:26
 * @description: 打印模版
 */
@RestController
@RequestMapping("/labelTemplate")
public class LabelTemplateController extends BaseController {

    @Autowired
    private LabelTemplateService service;

    //@RequiresPermissions("labeltemplate:insert")
    @RequestMapping(value = "insertLabelTemplate")
    public AjaxResult insertLabelTemplate(
            @RequestParam(value = "data", required = false) String data) {
        LabelTemplate record = JsonUtil.fromJson(data, LabelTemplate.class);
        //Date dt = getDateNow();
        record.setCode(GenerateGUID.generateGUID());
        record.setCreateDate(null);
        record.setMakeBillMan(getUserName());
        record.setModifier(getUserName());
        record.setModifyDate(null);
        record.setDeleteFlag(false);
        service.insert(record);
        return new AjaxResult(record);
    }

    //@RequiresPermissions("labeltemplate:delete")
    @RequestMapping(value = "deleteLabelTemplate")
    public AjaxResult deleteLabelTemplate(
            @RequestParam(value = "id", required = false) int id) {
        LabelTemplate record = new LabelTemplate();
        record.setId(id);
        record.setDeleteFlag(true);
        record.setModifier(getUserName());
        record.setModifyDate(null);
        int row = service.updateByPrimaryKeySelective(record);
        return new AjaxResult(row);
    }

    //@RequiresPermissions("labeltemplate:update")
    @RequestMapping(value = "updateLabelTemplate")
    public AjaxResult updateLabelTemplate(
            @RequestParam(value = "data", required = false) String data) {
        LabelTemplate record = JsonUtil.fromJson(data, LabelTemplate.class);
        record.setCreateDate(null);
        record.setMakeBillMan(null);
        record.setModifyDate(null);
        record.setModifier(getUserName());
        record.setDeleteFlag(false);
        service.updateByPrimaryKeySelective(record);
        return new AjaxResult(record);
    }

    @RequestMapping(value = "listLabelTemplate")
    public AjaxResult listLabelTemplate(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "systemTemplateId", required = false) String systemTemplateId,
            @RequestParam(value = "labelTemplateTypeId", required = false) String labelTemplateTypeId,
            @RequestParam(value = "labelTemplateTypeCode", required = false) String labelTemplateTypeCode,
            @RequestParam(value = "labelTemplateTypeName", required = false) String labelTemplateTypeName,
            @RequestParam(value = "industryId", required = false) String industryId,
            @RequestParam(value = "industryCode", required = false) String industryCode,
            @RequestParam(value = "industryName", required = false) String industryName,
            @RequestParam(value = "labelDataFieldTypeId", required = false) String labelDataFieldTypeId,
            @RequestParam(value = "labelDataFieldTypeCode", required = false) String labelDataFieldTypeCode,
            @RequestParam(value = "labelDataFieldTypeName", required = false) String labelDataFieldTypeName,
            @RequestParam(value = "labelDataStorageId", required = false) String labelDataStorageId,
            @RequestParam(value = "labelDataStorageCode", required = false) String labelDataStorageCode,
            @RequestParam(value = "labelDataStorageName", required = false) String labelDataStorageName,
            @RequestParam(value = "printerId", required = false) String printerId,
            @RequestParam(value = "printerCode", required = false) String printerCode,
            @RequestParam(value = "printerName", required = false) String printerName,
            @RequestParam(value = "labelInfoId", required = false) String labelInfoId,
            @RequestParam(value = "labelInfoCode", required = false) String labelInfoCode,
            @RequestParam(value = "labelInfoName", required = false) String labelInfoName,
            @RequestParam(value = "dpi", required = false) String dpi,
            @RequestParam(value = "hotTimes", required = false) String hotTimes,
            @RequestParam(value = "systemFlag", required = false) Boolean systemFlag,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "enableFlag", required = false) Boolean enableFlag,
            @RequestParam(value = "templateContent", required = false) String templateContent,
            @RequestParam(value = "templateSequence", required = false) String templateSequence,
            @RequestParam(value = "templateContinue", required = false) String templateContinue,
            @RequestParam(value = "printerURI", required = false) String printerURI,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        LabelTemplateExample example = new LabelTemplateExample();
        example.setPageIndex(pageIndex);
        example.setPageSize(pageSize);
        if (StringUtils.isNotBlank(orderBy)) {
            example.setOrderByClause(orderBy);
        }
        LabelTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteFlagEqualTo(false);

        if (StringUtils.isNotBlank(code)) {
            criteria.andCodeLike(code);
        }
        if (StringUtils.isNotBlank(name)) {
            criteria.andNameLike(name);
        }
        if (StringUtils.isNotBlank(systemTemplateId)) {
            String[] split = systemTemplateId.split(",");
            if (split.length > 1) {
                List<Integer> systemTemplateIdList = new ArrayList<>();
                for (String s : split) {
                    systemTemplateIdList.add(Integer.parseInt(s));
                }
                criteria.andSystemTemplateIdIn(systemTemplateIdList);
            } else {
                criteria.andSystemTemplateIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(labelTemplateTypeId)) {
            String[] split = labelTemplateTypeId.split(",");
            if (split.length > 1) {
                List<Integer> labelTemplateTypeIdList = new ArrayList<>();
                for (String s : split) {
                    labelTemplateTypeIdList.add(Integer.parseInt(s));
                }
                criteria.andLabelTemplateTypeIdIn(labelTemplateTypeIdList);
            } else {
                criteria.andLabelTemplateTypeIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(labelTemplateTypeCode)) {
            criteria.andLabelTemplateTypeCodeLike(labelTemplateTypeCode);
        }
        if (StringUtils.isNotBlank(labelTemplateTypeName)) {
            criteria.andLabelTemplateTypeNameLike(labelTemplateTypeName);
        }
        if (StringUtils.isNotBlank(industryId)) {
            String[] split = industryId.split(",");
            if (split.length > 1) {
                List<Integer> industryIdList = new ArrayList<>();
                for (String s : split) {
                    industryIdList.add(Integer.parseInt(s));
                }
                criteria.andIndustryIdIn(industryIdList);
            } else {
                criteria.andIndustryIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(industryCode)) {
            criteria.andIndustryCodeLike(industryCode);
        }
        if (StringUtils.isNotBlank(industryName)) {
            criteria.andIndustryNameLike(industryName);
        }
        if (StringUtils.isNotBlank(labelDataFieldTypeId)) {
            String[] split = labelDataFieldTypeId.split(",");
            if (split.length > 1) {
                List<Integer> labelDataFieldTypeIdList = new ArrayList<>();
                for (String s : split) {
                    labelDataFieldTypeIdList.add(Integer.parseInt(s));
                }
                criteria.andLabelDataFieldTypeIdIn(labelDataFieldTypeIdList);
            } else {
                criteria.andLabelDataFieldTypeIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(labelDataFieldTypeCode)) {
            criteria.andLabelDataFieldTypeCodeLike(labelDataFieldTypeCode);
        }
        if (StringUtils.isNotBlank(labelDataFieldTypeName)) {
            criteria.andLabelDataFieldTypeNameLike(labelDataFieldTypeName);
        }
        if (StringUtils.isNotBlank(labelDataStorageId)) {
            String[] split = labelDataStorageId.split(",");
            if (split.length > 1) {
                List<Integer> labelDataStorageIdList = new ArrayList<>();
                for (String s : split) {
                    labelDataStorageIdList.add(Integer.parseInt(s));
                }
                criteria.andLabelDataStorageIdIn(labelDataStorageIdList);
            } else {
                criteria.andLabelDataStorageIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(labelDataStorageCode)) {
            criteria.andLabelDataStorageCodeLike(labelDataStorageCode);
        }
        if (StringUtils.isNotBlank(labelDataStorageName)) {
            criteria.andLabelDataStorageNameLike(labelDataStorageName);
        }
        if (StringUtils.isNotBlank(printerId)) {
            String[] split = printerId.split(",");
            if (split.length > 1) {
                List<Integer> printerIdList = new ArrayList<>();
                for (String s : split) {
                    printerIdList.add(Integer.parseInt(s));
                }
                criteria.andPrinterIdIn(printerIdList);
            } else {
                criteria.andPrinterIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(printerCode)) {
            criteria.andPrinterCodeLike(printerCode);
        }
        if (StringUtils.isNotBlank(printerName)) {
            criteria.andPrinterNameLike(printerName);
        }
        if (StringUtils.isNotBlank(labelInfoId)) {
            String[] split = labelInfoId.split(",");
            if (split.length > 1) {
                List<Integer> labelInfoIdList = new ArrayList<>();
                for (String s : split) {
                    labelInfoIdList.add(Integer.parseInt(s));
                }
                criteria.andLabelInfoIdIn(labelInfoIdList);
            } else {
                criteria.andLabelInfoIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(labelInfoCode)) {
            criteria.andLabelInfoCodeLike(labelInfoCode);
        }
        if (StringUtils.isNotBlank(labelInfoName)) {
            criteria.andLabelInfoNameLike(labelInfoName);
        }
        if (StringUtils.isNotBlank(dpi)) {
            String[] split = dpi.split(",");
            if (split.length > 1) {
                List<Integer> dpiList = new ArrayList<>();
                for (String s : split) {
                    dpiList.add(Integer.parseInt(s));
                }
                criteria.andDpiIn(dpiList);
            } else {
                criteria.andDpiEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(hotTimes)) {
            String[] split = hotTimes.split(",");
            if (split.length > 1) {
                List<Integer> hotTimesList = new ArrayList<>();
                for (String s : split) {
                    hotTimesList.add(Integer.parseInt(s));
                }
                criteria.andHotTimesIn(hotTimesList);
            } else {
                criteria.andHotTimesEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (systemFlag != null) {
            criteria.andSystemFlagEqualTo(systemFlag);
        }
        if (StringUtils.isNotBlank(appId)) {
            String[] split = appId.split(",");
            if (split.length > 1) {
                List<Integer> appIdList = new ArrayList<>();
                for (String s : split) {
                    appIdList.add(Integer.parseInt(s));
                }
                criteria.andAppIdIn(appIdList);
            } else {
                criteria.andAppIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (enableFlag != null) {
            criteria.andEnableFlagEqualTo(enableFlag);
        }
        if (StringUtils.isNotBlank(templateContent)) {
            criteria.andTemplateContentLike(templateContent);
        }
        if (StringUtils.isNotBlank(templateSequence)) {
            criteria.andTemplateSequenceLike(templateSequence);
        }
        if (StringUtils.isNotBlank(templateContinue)) {
            criteria.andTemplateContinueLike(templateContinue);
        }
        if (StringUtils.isNotBlank(printerURI)) {
            criteria.andPrinterURILike(printerURI);
        }
        if (StringUtils.isNotBlank(startDate)) {
            criteria.andCreateDateGreaterThanOrEqualTo(DateTime.parse(startDate, format).toDate());
        }
        if (StringUtils.isNotBlank(endDate)) {

            criteria.andCreateDateLessThanOrEqualTo(DateTime.parse(endDate, format).toDate());
        }

        List<LabelTemplate> list = service.listByExample(example);
        Pagination<LabelTemplate> pList = new Pagination<>(example, list, example.getCount());
        return new AjaxResult(pList);
    }

    @RequestMapping(value = "getLabelTemplate")
    public AjaxResult getLabelTemplate(
            @RequestParam(value = "id", required = false) Integer id) {
        LabelTemplate res = service.getByPrimaryKey(id);
        return new AjaxResult(res);
    }

    @RequestMapping(value = "copyLabelTemplate")
    public AjaxResult copyLabelTemplate(
            @RequestParam(value = "data", required = false) String data) {
        LabelTemplate record = JsonUtil.fromJson(data, LabelTemplate.class);
        LabelTemplate labelTemplate = service.getByPrimaryKey(record.getId());
        if (labelTemplate == null) {
            return new AjaxResult(-1, "模版不存在");
        }
        Boolean systemFlag = labelTemplate.getSystemFlag() != null ? labelTemplate.getSystemFlag() : false;
        labelTemplate.setId(0);
        labelTemplate.setAppId(getAppId());
        labelTemplate.setSystemFlag(systemFlag);
        labelTemplate.setName(record.getName() != null ? record.getName() : labelTemplate.getName());
        labelTemplate.setCreateDate(null);
        labelTemplate.setMakeBillMan(null);
        labelTemplate.setModifyDate(null);
        labelTemplate.setModifier(getUserName());
        labelTemplate.setDeleteFlag(false);
        int rows = service.insert(labelTemplate);
        /*if (rows > 0) {
            LabelDataFieldType labelDataFieldType = labelDataFieldTypeService.getByPrimaryKey(labelTemplate.getLabelDataFieldTypeId());
            labelDataFieldType.setId(0);
            labelDataFieldType.setAppId(getAppId());
            labelDataFieldType.setSystemFlag(systemFlag);
            labelDataFieldType.setCreateDate(null);
            labelDataFieldType.setMakeBillMan(getUserName());
            labelDataFieldType.setModifier(getUserName());
            labelDataFieldType.setModifyDate(null);
            labelDataFieldType.setDeleteFlag(false);
            labelDataFieldTypeService.insert(labelDataFieldType);
            LabelDataFieldExample example = new LabelDataFieldExample();
            example.setPageIndex(1);
            example.setPageSize(99);
            LabelDataFieldExample.Criteria criteria = example.createCriteria();
            criteria.andDeleteFlagEqualTo(false);
            criteria.andLabelDataFieldTypeIdEqualTo(labelTemplate.getLabelDataFieldTypeId());
            List<LabelDataField> list = labelDataFieldService.listByExample(example);
            if (list != null && list.size() > 0) {
                for (LabelDataField labelDataField : list) {
                    labelDataField.setId(0);
                    labelDataField.setAppId(getAppId());
                    labelDataField.setLabelDataFieldTypeId(labelDataFieldType.getId());
                    labelDataField.setSystemFlag(systemFlag);
                    labelDataField.setCreateDate(null);
                    labelDataField.setMakeBillMan(getUserName());
                    labelDataField.setModifier(getUserName());
                    labelDataField.setModifyDate(null);
                    labelDataField.setDeleteFlag(false);
                    labelDataFieldService.insert(labelDataField);
                }
            }
        }*/
        return new AjaxResult(labelTemplate);
    }

}